package input;

import instance.Instance;
import instance.InstanceEj1;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import java.util.LinkedList;
import java.util.StringTokenizer;

public class Problem1Input implements Input{
	
	
	private String inputFile;
	private LinkedList<Instance> instances;
	
	public Problem1Input(String inputFile) {
		this.inputFile = inputFile;

	}
    //nos guardamos todas las instancias del archivo de entrada para procesarlos de a uno
	
	@Override
	public void readFile() throws Exception {
				
		BufferedReader bufferedReader = new BufferedReader(new FileReader(
				new File(inputFile)));

		instances = new LinkedList<Instance>();

		while (bufferedReader.ready()) {
			Instance instance =  readInstance(bufferedReader);
			instances.add( instance );
		}
		
		bufferedReader.close();	
	}

	@Override
	public LinkedList<Instance> getInstances() {
	
		return instances;
	}

	@Override
	public Instance readInstance(BufferedReader bufferedReader)
			throws Exception {
		InstanceEj1 subInstance ;
		String string = bufferedReader.readLine();
				//delimitador espacio en blanco por defecto
		StringTokenizer st = new StringTokenizer( string );
		int size = st.countTokens();
		int[] instance = new int[size];
		for (int i=0; i<size && st.hasMoreTokens(); i++){
			instance[i] = Integer.parseInt( st.nextToken().trim() );
		}
		subInstance = new InstanceEj1(instance, size);			
		return subInstance;
	}
}
